package com.agent.crm.db;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import xu.ye.view.initData.TypeData;

import android.R.integer;

public class DBInitScript {

    // 用户表
    public static String Table_User = "t_user";

    public static HashMap<String, Class<?>> user_column = new HashMap<String, Class<?>>() {
        {
            put("user_id", Integer.class);
            put("user_name", String.class);
            put("user_password", String.class);
        }
    };
    // 联系人信息表
    public static String Table_ContactsInfo = "t_contacts_info";
    public static HashMap<String, Class<?>> contactsInfo_column = new HashMap<String, Class<?>>() {
        {
            put("contacts_id", Integer.class);
            put("name", String.class);// 姓名
            put("age", Integer.class);// 年龄
            put("birth_date", String.class);//出生年月
            put("job", String.class);// 工作和职位
            put("address", String.class);// 住址
            put("email", String.class);//email
            put("sex", String.class);// 性别
            put("favious", String.class);// 喜好
            put("company", String.class);// 公司
//          put("groupId", String.class);// 分类Id
//          put("typeId", String.class);// 分组Id
            put("remarks", String.class);// 备注
            put("family_phone", String.class);// 家庭电话
            put("office_phone", String.class);//办公电话
            put("mobile_phone", String.class);// 手机
            put("sort_key", String.class);// 排序字段
            put("lookup_key", String.class);
            put("photoid", Long.class);
            put("sys_contactid", Integer.class);//存储系统联系人主键
            put("is_attention", Integer.class);//是否关注此人：0 不关注 1 关注
        }
    };
    //联系人员分组中间表
    public static String Table_Group_Middle = "t_group_middle";
    public static HashMap<String, Class<?>> groupMiddle_column = new HashMap<String, Class<?>>() {
        {
            put("gc_id", Integer.class);
            put("groupid", Integer.class);// 分组主键
            put("contactsid", Integer.class);//联系人主键
        }
    };
    
    
    // 联系人分组表
    public static String Table_GroupInfo = "t_group_info";
    public static HashMap<String, Class<?>> groupInfo_column = new HashMap<String, Class<?>>() {
        {
            put("group_id", Integer.class);
            put("group_name", String.class);// 组名
            put("group_count", Integer.class);//数量
        }
    };
    //联系人分类中间表
    public static String Table_Type_Middle = "t_type_middle";
    public static HashMap<String, Class<?>> typeMiddle_column = new HashMap<String, Class<?>>() {
        {
            put("tc_id", Integer.class);
            put("typeid", Integer.class);// 分类主键
            put("contactsid", Integer.class);//联系人主键
        }
    };
    
    
    
    // 联系人分类表
    public static String Table_TypeInfo = "t_type_info";// 联系人实际分类表（即详细分类表）
    public static HashMap<String, Class<?>> typeInfo_column = new HashMap<String, Class<?>>() {
        {
            put("type_id", Integer.class);
            put("big_typeId", Integer.class);// 大类Id
            put("type_name", String.class);// 详细分类的分类名称
            put("sys_config", Integer.class);//是否系统配置；0：否  1：是
        }
    };
    public static String Table_BigTypeInfo = "t_bigtype_info";// 分类的大类表
    public static HashMap<String, Class<?>> bigTypeInfo_column = new HashMap<String, Class<?>>() {
        {
            put("bigType_id", Integer.class);
            put("bigtype_name", String.class);// 大类名称
        }
    };
    // 任务明细表
    public static String Table_Schedule = "t_schedule";// 任务表
    public static HashMap<String, Class<?>> schedule_column = new HashMap<String, Class<?>>() {
        {
            put("schedule_id", Integer.class);
            put("scheduleTypeID", Integer.class);
            put("remindID", Integer.class);
            put("schedule_title", String.class);// 任务标题
            put("schedule_type", Integer.class);// 一般任务(或定时任务，只需标记任务时间)、周期任务、 (0 为非周期任务 1为周期任务)
            put("schedule_content", String.class);// 任务内容
            put("schedule_date", String.class);// 任务日期
            put("schedule_opration", String.class);// 短信、提醒(SMS,remind)
            put("schedule_status", String.class);// 任务状态（未开始 0 ，进行中1 ，已结束 2）
            put("schedule_start_date", String.class);// 任务启动日期
            put("schedule_start_time", String.class);// 任务启动时间
            put("schedule_end_date", String.class);// 任务结束日期
            put("schedule_end_time", String.class);// 任务结束时间
            put("schedule_mode", String.class);// 任务模式(单个人员任务，分类人员任务，群组人员任务)
            put("schedule_objectId", String.class);// 任务对象(人员ID，分类ID，群组ID)
            put("schedule_cycle_type", String.class);// 任务周期（每月，每周，每天，执行完周期任务后，自动更新启动日期，指向下一个周期来达到周期的目的，如日周期+1 ，周周期+7，月周期指向下一个月）
        }
    };
    
    public static String Table_ScheduleTagDate = "t_schedule_tag_date";// 任务表
    public static HashMap<String, Class<?>> schedule_tag_date_column = new HashMap<String, Class<?>>() {
        {
            put("tag_id", Integer.class);
            put("year", Integer.class);
            put("month", Integer.class);
            put("day", Integer.class);
            put("scheduleID", Integer.class);
        }
    };
    
    public static String Table_SysParam = "t_sys_param";
    public static HashMap<String, Class<?>> sysParam_column = new HashMap<String, Class<?>>() {
        {
            put("param_id", Integer.class);
            put("belongToTable", String.class);
            put("param_content", String.class);
        }
    };
    
    public static String Table_NoteBook = "t_note_book";
    public static HashMap<String, Class<?>> noteBook_column = new HashMap<String, Class<?>>() {
        {
            put("noteBook_id", Integer.class);
            put("noteItemTitle", String.class);
            put("noteItemTitleContent", String.class);
            put("noteItemLimitDate", String.class);
        }
    };
    

    private static String formatSqlStr(String tableName,
            HashMap<String, Class<?>> tableColumn) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("CREATE TABLE IF NOT EXISTS ");
        buffer.append(tableName + " (");
        Iterator iter = tableColumn.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            String key = entry.getKey().toString();
            if (key.contains("_id")) {
                buffer.append(" " + key + " integer primary key autoincrement,");
            } else {
                buffer.append(" " + key + " "
                        + getColumnType((Class<?>) entry.getValue()) + ",");
            }
        }
        buffer.deleteCharAt(buffer.length() - 1);
        buffer.append(")");
        return buffer.toString();
    }

    private static String getColumnType(Class<?> classTpye) {
        if (classTpye.equals(Integer.class)) {
            return "integer";
        }
        if (classTpye.equals(String.class)) {
            return "varchar";
        }
        if (classTpye.equals(Date.class)) {
            return "varchar";
        }
        return "varchar";
    }

    public static List<String> DBScript = new ArrayList<String>() {
        {
            add(formatSqlStr(Table_User, user_column));
            add(formatSqlStr(Table_ContactsInfo, contactsInfo_column));
            add(formatSqlStr(Table_GroupInfo, groupInfo_column));
            add(formatSqlStr(Table_TypeInfo, typeInfo_column));
            add(formatSqlStr(Table_BigTypeInfo, bigTypeInfo_column));
            add(formatSqlStr(Table_Schedule,schedule_column));
            add(formatSqlStr(Table_SysParam, sysParam_column));
            add(formatSqlStr(Table_ScheduleTagDate,schedule_tag_date_column));
            add(formatSqlStr(Table_Group_Middle,groupMiddle_column));
            add(formatSqlStr(Table_NoteBook,noteBook_column));
            add(formatSqlStr(Table_Type_Middle,typeMiddle_column));
            if(0!=TypeData.typeArray.length){
            	addAll(initTypeInfoData());
            }
        }
    };
    
    private static List<String> initTypeInfoData(){
    	List<String> list =new ArrayList<String>();
    	for (int i = 0; i < TypeData.typeArray.length; i++) {
    		String sql ="insert into " +Table_TypeInfo+" (type_name,sys_config) values ('"+TypeData.typeArray[i]+"','1')";
    		list.add(sql);
		}
    	return list;
    }

    public static void main(String[] args) {
        List<String> list = DBScript;
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
    }
}
